<?php

namespace app\bll;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use support\Db;

/**
 * 钩子
 */
class Hook
{

    /**
     * 根据 ID 查找钩子记录
     *
     * @param int $id 钩子记录的 ID
     * @return array 返回找到的钩子记录数组，如果未找到则返回空数组
     */
    public function find(int $id): array
    {
        // 从 frame_hook 表中查询 ID 等于传入参数 $id 的第一条记录
        $data =  Db::table('frame_hook')
            ->where('id', '=', $id)
            ->first();
        // 如果查询结果为空，则返回空数组；否则将对象转换为数组并返回
        return $data === null ? [] : get_object_vars($data);
    }

    /**
     * 根据标识查找
     *
     * @param string $ident
     * @return array
     */
    public function findByIdent(string $ident): array
    {
        $data =  Db::table('frame_hook')
            ->where('ident', '=', $ident)
            ->first();
        return $data === null ? [] : get_object_vars($data);
    }

    /**
     * 获取钩子
     *
     * @return array
     */
    public function select(): array
    {
        return  Db::table('frame_hook')->get()
            ->map(function ($value) {
                return (array)$value;
            })
            ->toArray();
    }


    /**
     * 批量插入钩子
     *
     * @param array $data
     * @return void
     */
    public function batchAdd(array $data)
    {
        return Db::table('frame_hook')->insert($data);
    }


    /**
     * 获取钩子 按层级关系显示
     *
     * @param boolean $tojson
     * @return array|string
     */
    public function cascader(bool $tojson = true): array|string
    {

        $bllPlugin = new Plugin();
        $plugins = $bllPlugin->select();

        foreach ($plugins as $key => $plugin) {
            $plugins[$key][$plugin['ident']] = [
                'ident' => $plugin['ident'],
                'title' => $plugin['title']
            ];
        }
        $plugins = array_column($plugins, null, 'ident');
        $hooks = $this->select();

        $plugins['frame'] = [
            'title' => '框架本身',
            'ident' => 'frame',
        ];
        foreach ($hooks as $akey => $hook) {
            if (isset($plugins[$hook['plugin']])) {
                $plugins[$hook['plugin']]['children'][] = [
                    'title' => $hook['title'],
                    'ident' => $hook['ident'],
                ];
            }
        }

        foreach ($plugins as $key => $plugin) {
            if (!isset($plugin['children'])) {
                unset($plugins[$key]);
            }
        }

        $plugins = array_values($plugins);
        return $tojson == true ? json_encode($plugins, JSON_UNESCAPED_UNICODE) : $plugins;
    }

    /**
     * 分页查询
     *
     * @param string $title 标题
     * @param string $ident 标识
     * @param string $plugin 插件
     * @param integer $pagesize
     * @return LengthAwarePaginator
     */
    public function paginator(
        string $title = '',
        string $ident = '',
        string $plugin = '',
        int $pagesize = 20
    ): LengthAwarePaginator {
        return  Db::table('frame_hook')
            ->when($title != '', function ($query) use ($title) {
                return $query->where('title', 'like', '%' . $title . '%');
            })
            ->when($ident != '', function ($query) use ($ident) {
                return $query->where('ident', 'like', '%' . $ident . '%');
            })
            ->when($plugin != '', function ($query) use ($plugin) {
                return $query->where('plugin', '=',  $plugin);
            })
            ->paginate();
    }
}
